11. Ranking in Zipline

Explore the rank function

The Returns class inherits from zipline.pipeline.factors.factor.
The documentation for rank is located here and is also pasted below:

rank(method='ordinal', ascending=True, mask=sentinel('NotSpecified'), groupby=sentinel('NotSpecified'))[source]
Construct a new Factor representing the sorted rank of each column within each row.

Parameters:

method (str, {'ordinal', 'min', 'max', 'dense', 'average'}) – The method used to assign ranks to tied elements. See scipy.stats.rankdata for a full description of the semantics for each ranking method. Default is ‘ordinal’.

ascending (bool, optional) – Whether to return sorted rank in ascending or descending order. Default is True.

mask (zipline.pipeline.Filter, optional) – A Filter representing assets to consider when computing ranks. If mask is supplied, ranks are computed ignoring any asset/date pairs for which mask produces a value of False.

groupby (zipline.pipeline.Classifier, optional) – A classifier defining partitions over which to perform ranking.

Returns:
ranks – A new factor that will compute the ranking of the data produced by self.

Return type:
zipline.pipeline.factors.Rank

By looking at the documentation, and the link to scipy.stats.rankdata (also pasted below), which option for parameter method would we choose if we want unique ranks associated with each stock, even when the values are tied?

Note When the documentation refers to "tied" values, it means instances where there are two alpha values for two different assets that are the same number, so there are different ways to handle the "tied" values when converting those values into ranks.

‘average’:
The average of the ranks that would have been assigned to all the tied values is assigned to each value.

‘min’:
The minimum of the ranks that would have been assigned to all the tied values is assigned to each value. (This is also referred to as “competition” ranking.)

‘max’:
The maximum of the ranks that would have been assigned to all the tied values is assigned to each value.

‘dense’:
Like ‘min’, but the rank of the next highest element is assigned the rank immediately after those assigned to the tied elements.

‘ordinal’:
All values are given a distinct rank, corresponding to the order that the values occur in a.

ranking options

By looking at the documentation, and scipy.stats.rankdata, which option for parameter method would we choose if we want unique ranks associated with each stock, even when the values are tied?

SOLUTION: ordinal

Assuming a stock universe of 100 stocks, when a higher raw alpha value indicates a higher return, we have rank 1 for the most negative alpha value, and 100 for the most positive alpha value. When a lower raw alpha value indicates a higher return, which parameter can you use to choose a rank of 100 for the most negative value and 1 for the most positive value?

SOLUTION: ascending = False